<?php
/**
 * 用户管理类
 * 
 * @author  Lee<a605333742@gmail.com>
 * @time    2016-07-25
 */
namespace Admin\Controller;

class UserController extends CommonController{
    /*
     * 构造函数
     * 
     * @return #
     */
    public function _initialize(){       
        parent::_initialize();
        $this->db   =new \Model\UserModel();
    }
    
    /*
     * 正常企业用户列表
     * 
     * @return  #
     */
    public function index(){
        if(I("request.nickName")){
            $map['nickName']    =array("like","%".I("request.nickName","",'trim')."%");
        }
        if(I("request.companyName")){
            $map['companyName'] =array("like","%".I("request.companyName",'',"trim")."%");
        }
        if(I("request.add_time")){
            $map['add_time']    =array("egt",I("request.add_time"));
        }      
        $map['type']    =1;
        $map['state']   =2;
        $sortBy         ='add_time';
        $sort           =desc;
        $userList       =$this->_list($this->db, $map,$sortBy,$sort);
        $this->assign("userList",$userList);
        $this->assign("act",1);
        $this->display();
    }
    
    /*
     * 待审核企业用户
     * 
     * @return #
     */
    public function auditUser(){
        $map['type']    =1;
        $map['state']   =1;
        $sortBy         ='add_time';
        $sort           =desc;
        $userList       =$this->_list($this->db, $map,$sortBy,$sort);
        $this->assign("userList",$userList);
        $this->assign("act",2);
        $this->display("index");
    }
    
    /*
     * 个人用户列表
     * 
     * @return  #
     */
    
    public function inUser(){
        $map['type']    =0;
        $map['state']   =2;
        //区别标志
        $sortBy         ='add_time';
        $sort           =desc;
        $userList       =$this->_list($this->db, $map,$sortBy,$sort);
        $this->assign("userList",$userList);
        $this->assign("act",3);
        $this->display("index");
    }
    
    /*
     * 已禁用的用户列表，包括个人和企业
     * 
     * @return #
     */
    public function noUser(){
        $map['state']   =3;
        $sortBy         ='add_time';
        $sort           =desc;
        $userList       =$this->_list($this->db, $map,$sortBy,$sort);
        $this->assign("userList",$userList);
        $this->assign("act",4);
        $this->display("index");
    }
    
    /*
     * 显示编辑用户
     * 
     * @return #
     */
    public function show(){
        $id     =I("request.id","","int");
        $region =D("Region")->select();
        $this->assign("region",$region);
        if(!$id){
            $this->display();
        }else{
            $userInfo   =$this->db->find($id);
            $this->assign("userInfo",$userInfo);
            $this->display();
        }
    } 
    
    /*
     * 编辑用户
     * 
     * @return #
     */
    public function edit(){
        if(!empty($_POST)){   
            if(I("post.id")){
                $map['id']        =I("post.id");  
                $map2['id']       =I("post.id");
            }
            if(I("post.nickName")){
                $map['nickName']=I("post.nickName");
            }else{
                $this->error("昵称不能为空");
            }
            if(I("post.password")){
                $map['password']=I("post.password","","md5");
            }else if(!I("post.password") && !I("post.id")){
                $this->error("密码不能为空");
            }
            if(I("post.tel")){
                $map['tel']     =I("post.tel");
            }
            if(I("post.sex")){
                $map['sex']     =I("post.sex");
            }
            if(I("post.type")){
                $map['type']    =I("post.type");
            }else{
                $map['type']    =0;
            }
            if(I("post.companyName")){
                $map['companyName'] =I("post.companyName","","trim");
            }
            if(I("post.regionId")){
                $map['regionId']    =I("post.regionId","","int");
            }
            if(I("post.integral")){
                $map['integral']    =I("post.integral",'',"int");
            }
            $map['state']           =I("post.state","","int");
            /*
            if(!empty($_FILES['icon']['name'])){
                //编辑用户，如果头像更改，则删除原头像，减少空间占用
                $photo              =D("User")->where(array("id"=>I("post.id","","int")))->getField("icon");
                $_SESSION['icon']   =$photo;
                $config =array(
                    'rootPath'  =>'./Application/Public/',                              //根目录
                    'savePath'  =>'upload/',                                            //保存路径
                    'maxSize'   =>'902400',                                             //上传的文件大小限制 (0-不做限制)
                    'exts'      =>array('jpg','png','gif','jpeg',"JPG","PNG","JPEG","GIF")    //允许上传的文件后缀
                );
                //附件被上传到路径：根目录/保存目录路径/创建日期目录
                $new_upload =new \Think\Upload($config);
                //uploadOne会返回已经上传的附件信息
                $upload     =$new_upload->uploadOne($_FILES['icon']);
                if(!$upload){
                     $new_upload->getError();           //获得上传附件产生的错误信息
                     $this->error();
                }else{
                    //拼装图片的路径名
                    $upimg      =$upload['savepath'].$upload['savename'];
                    $map['icon'] =$upimg;                                         
                }
            }
            show_bug($map);exit;
             * 
             */
            $userEdit       =$this->db->edit($map);
            unlink('Application/Public/'.$_SESSION['icon']);            
            if($userEdit){ 
                $this->success('添加成功');              
            }else{
                $this->error("编辑失败");
            }
        }else{
            $this->error("编辑失败");
        }
    }
    
    /*
     * 删除指定用户，批量删除用户
     * 
     * @return bool
     */
    public function del(){ 
        if(is_array(I("request.id"))){           
            $id =array("In",implode(",", I("request.id")));
        }else{            
            $id =I("request.id");
        }
        $map['id']      =$id;
        $map['state']   =4;
        if($this->db->edits($map)){            
            $this->success("删除成功",U("index"));
        }else{
            $this->error("删除失败",U("index"));
        }
    }
    
    /*
     * 禁用，批量禁用数据
     * 
     * @return #
     */
    public function noUsers(){
        if(is_array(I("request.id"))){
            $id =array("In"=>implode(",", I("request.id")));
        }else{
            $id =I("request.id");
        } 
        $map['id']      =$id;
        $map['state']   =3;
        if($this->db->edits($map)){
            $this->success("用户禁用成功",U("index"));
        }else{
            $this->error("禁用失败",U("index"));
        }
    }
    
    /*
     * 审核，批量审核，启用，批量启用
     * 
     * @return #
     */
    public function allEdit(){
        if(is_array(I("request.id"))){
            $map['id']  =array("In"=>implode(",", I("request.id")));
        }else{
            $map['id'] =I("request.id");
        }
        $map['state']   =2;       
        if($this->db->edits($map)){
            $this->success("用户审核成功",U("index"));
        }else{
            $this->error("用户审核失败",U("index"));
        }
    }
    
    /*
     * 待审核，批量待审
     * 
     * @return #
     */
    public function audUser(){
        if(is_array(I("request.id"))){
            $id =array("In"=>implode(",", I("request.id")));
        }else{
            $id =I("request.id");
        } 
        $map['id']      =$id;
        $map['state']   =1;
        if($this->db->edits($map)){
            $this->success("用户待审核设置成功",U("index"));
        }else{
            $this->error("用户待审核设置失败",U("index"));
        }
    }
}

